<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-input
        placeholder="请输入内容"
        v-model="orderInfo.query"
        class="el_input"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          :disabled="!orderInfo.query"
        ></el-button>
      </el-input>
      <!-- 表格 -->
      <el-table style="width: 100%; margin: 20px 0" border :data="orderList">
        <el-table-column type="index" label="#" width="70" align="center">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="width" align="center">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="100" align="center">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100" align="center">
          <template v-slot="{ row }">
            <el-tag type="danger" v-if="row.pay_status == 0">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100" align="center">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="150" align="center">
        </el-table-column>
        <el-table-column prop="prop" label="操作" width="120" align="center">
          <template v-slot="{ row }">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editOrder(row)"> 
            </el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="viewLogistics(row)">   
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器  -->
      <el-pagination
        align="center"
        :current-page="orderInfo.pagenum"
        :page-sizes="[3, 6, 9]"
        :page-size="orderInfo.pagesize"
        layout=" sizes,total,prev, pager, next, jumper"
        :total="total"
        @current-change="getOrderList"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </el-card>
    <!-- 修改按钮弹出的对话框 :before-close="dialogBeforeClose"-->
    <el-dialog title="修改订单" :visible.sync="editOrderVisible" width="40%" @close="closeDialog">
      <el-form :model="orderForm" :rules="rules" ref="formRef" label-width="100px"
       class="demo-ruleForm">
        <el-form-item label="省市区/县" prop="city">
        <!-- 多级选择器 @change="handleChange"-->
          <el-cascader v-model="orderForm.city" :options="cityData" :props="props"
            >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="orderForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="editOrderVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrderVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看物流信息的对话框 -->
    <el-dialog title="查看物流" :visible.sync="logisticsVisible" width="width">
        <!-- 因为element版本低于2.6不能使用时间线这个组件 -->
        <el-timeline :reverse="reverse">
            <el-timeline-item
            v-for="(item, index) in data"
            :key="index"
            :timestamp="item.time">
            {{item.contex}}
            </el-timeline-item>
        </el-timeline>
        <div slot="footer">
            <el-button @click="logisticsVisible = false">取 消</el-button>
            <el-button type="primary" @click="logisticsVisible = false">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from "dayjs"
import cityData from '../../api/citydata.js'
import data from '../../api/data.js'

export default {
  name: "order",
  data() {
    return {
      total: 10,
      orderInfo: {
        query: "", // 搜索的值
        pagenum: 1, //当前页
        pagesize: 3,
        // user_id: '',//用户id
        // pay_status
        // is_send
        // order_fapiao_title
      },
      //    储存订单列表数据
      orderList: [],
      //    控制对话框显隐
      editOrderVisible: false,
      //    表单验证
      rules: {
        city: [
          { required: true, message: "请选择省份或县", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入地址", trigger: "blur" },
        ],
      },
      //   收集修改的表单数据
      orderForm:{
        address:'',
        // 多级选择器
        city:[]
      },
      //   多级选择器的要展示数据
      cityData,
      data,
      //   多级选择器的配置项
      props:{ 
        expandTrigger: 'hover' ,
        value: 'value',
        label: 'label',
        children: 'children'
      },
    //   查看物流的对话框
    logisticsVisible: false,
    reverse: true,
    }
  },
  methods: {
    // 获取订单列表数据
    async getOrderList(pager = 1) {
      // 但前页关闭时
      this.orderInfo.pagenum = pager
      let { data: result } = await this.$API.get("orders", {
        params: this.orderInfo
      })
      if (result.meta.status !== 200) {
        return this.$message.error("请求数据失败");
      }
      result.data.goods.forEach((item) => {
        item.create_time = dayjs(item.create_time).format("YYYY-MM-DD HH:mm")
      })
      this.orderList = result.data.goods
      this.total = result.data.total
    },
    // 改变展示条数时触发
    handleSizeChange(limit) {
      this.orderInfo.pagesize = limit
      this.getOrderList()
    },
    // 修改订单按钮
    editOrder(row) {
      this.editOrderVisible = true
    },
    // 关闭对话框的回调
    closeDialog(){
        // 清空表单数据
        this.$refs.formRef.resetFields()
    },
    // 查看物流按钮
    async viewLogistics(row){
        this.logisticsVisible = true
    }
  },
  mounted() {
    this.getOrderList()
  },
}
</script>

<style>
.el_input {
  width: 300px;
}
.el-cascader-panel{
    height: 220px;
}
</style>